package com.lu.adog.demo.main.page.image.dogitem;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.lu.adog.R;
import com.lu.adog.anim.OnAnimationEndListener;
import com.lu.adog.anim.OnAnimationStartListener;
import com.lu.adog.demo.common.CommonActivity;
import com.lu.adog.exception.FileCanNotReadException;
import com.lu.adog.util.Logg;
import com.lu.adog.util.image.BitmapUtil;
import com.lu.adog.util.image.PSGray;

/**
 * ps黑白处理zhong
 *
 * @author Yue
 * @date 2017/11/16 16:56
 */
public class PSBWColorActivity extends CommonActivity {
    private static final int REQUEST_CODE = 200;
    private ImageView ivImagePsbw;
    private LinearLayout layoutBottomPsw;
    private SeekBar sbRedPsw;
    private SeekBar sbYellowPsw;
    private SeekBar sbGreenPsw;
    private SeekBar sbCyanPsw;
    private SeekBar sbBluePsw;
    private SeekBar sbMagentaPsw;
    private ImageButton ibMenuMorePsw;
    private View layoutBottomRoot;
    private View ibMenuDown;
    private TextView tvRecoveryColor;
    private TextView tvMakeGray;
    private Bitmap colorBitmap;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_psbw);
        assignViews();

    }

    private void assignViews() {
        ivImagePsbw = (ImageView) findViewById(R.id.iv_image_psbw);
        layoutBottomPsw = (LinearLayout) findViewById(R.id.layout_bottom_psw);
        sbRedPsw = (SeekBar) findViewById(R.id.sb_red_psw);
        sbYellowPsw = (SeekBar) findViewById(R.id.sb_yellow_psw);
        sbGreenPsw = (SeekBar) findViewById(R.id.sb_green_psw);
        sbCyanPsw = (SeekBar) findViewById(R.id.sb_cyan_psw);
        sbBluePsw = (SeekBar) findViewById(R.id.sb_blue_psw);
        sbMagentaPsw = (SeekBar) findViewById(R.id.sb_magenta_psw);
        ibMenuMorePsw = (ImageButton) findViewById(R.id.ib_menuMore);
        layoutBottomRoot = findViewById(R.id.layout_bottom_root);
        ibMenuDown = findViewById(R.id.ib_menuDown);
        tvMakeGray = (TextView) findViewById(R.id.tv_makeGray);
        tvRecoveryColor = (TextView) findViewById(R.id.tv_recovery);

        ibMenuMorePsw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBottomMenu();
            }
        });
        ibMenuDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideBottomMenu();
            }
        });

        tvMakeGray.setOnClickListener(new View.OnClickListener() {
            private boolean isDisable = false;

            @Override
            public void onClick(View v) {

                if (isDisable) return;
                isDisable = true;
                ivImagePsbw.post(new Runnable() {
                    @Override
                    public void run() {
                        long nowTime = System.currentTimeMillis();
                        long oldTime = nowTime;

                        Logg.e(">>>", "开始去色");
                        Bitmap grayBitmap = PSGray.createBlackWhiteImage(colorBitmap);
                        nowTime = System.currentTimeMillis();
                        Logg.e(">>>", "去色耗时：" + (nowTime - oldTime));
                        oldTime = nowTime;

                        ivImagePsbw.setBackgroundDrawable(new BitmapDrawable(grayBitmap));
                        nowTime = System.currentTimeMillis();
                        Logg.e(">>>", "渲染耗时：" + (nowTime - oldTime));

                        isDisable = false;
                    }
                });

            }
        });
        tvRecoveryColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ivImagePsbw.setBackgroundDrawable(new BitmapDrawable(colorBitmap));
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_psw, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_seletePicture:
//打开系统相册
                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, REQUEST_CODE);
                break;
            case R.id.menu_blacWhite:
                showBottomMenu();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private ObjectAnimator oaShow;
    private ObjectAnimator oaHide;

    private void showBottomMenu() {
        if (oaHide != null && oaHide.isRunning()) {
            return;
        }
        layoutBottomRoot.setVisibility(View.VISIBLE);
//        layoutBottomPsw.setVisibility(View.VISIBLE);
        oaShow = ObjectAnimator.ofFloat(layoutBottomPsw, "translationY", layoutBottomPsw.getMeasuredHeight(), 0);
        oaShow.setDuration(200);

        ObjectAnimator oaAlpha = ObjectAnimator.ofFloat(layoutBottomRoot, "alpha", 0.3f, 1f);
        oaAlpha.setDuration(200);

        oaAlpha.start();
        oaShow.start();
    }

    private void hideBottomMenu() {
        if (oaShow != null && oaShow.isRunning()) {
            return;
        }
        oaHide = ObjectAnimator.ofFloat(layoutBottomPsw, "translationY", 0, layoutBottomPsw.getMeasuredHeight());
        oaHide.setDuration(200);

        ObjectAnimator oaAlpha = ObjectAnimator.ofFloat(layoutBottomRoot, "alpha", 1f, 0f);
        oaAlpha.setDuration(200);
        oaAlpha.start();
        oaHide.start();


        oaHide.addListener(new OnAnimationStartListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                //                layoutBottomPsw.setVisibility(View.GONE);
                layoutBottomRoot.setVisibility(View.GONE);

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (REQUEST_CODE == requestCode && Activity.RESULT_OK == resultCode && null != data) {
            Uri selectImageUri = data.getData();
            String[] filePathColumn = new String[]{MediaStore.Images.Media.DATA};//要查询的列
            Cursor cursor = getContentResolver().query(selectImageUri, filePathColumn, null, null, null);
            String pirPath = null;
            while (cursor.moveToNext()) {
                pirPath = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));//所选择的图片路径
            }
            cursor.close();
            try {
                colorBitmap = BitmapUtil.loadBitmap(pirPath);
                ivImagePsbw.setBackgroundDrawable(new BitmapDrawable(colorBitmap));
            } catch (FileCanNotReadException e) {
                e.printStackTrace();
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

}
